package com.victor.pomelo.controller;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.victor.pomelo.model.Menu;
import com.victor.pomelo.model.Resource;
import com.victor.pomelo.service.MenuService;
import com.victor.pomelo.service.ResourceService;
import com.victor.pomelo.service.UserService;

@Controller
@RequestMapping(value = "/manager")
// 会员中心要被拦截
public class IndexController {
	@Autowired
	private UserService userService;

	@Autowired
	private MenuService menuService;
	@Autowired
	private ResourceService resourceService;

	@RequestMapping("/")
	public String loadMain() {
		return ("main");
	}

	@RequestMapping("/left")
	public String loadLeft(HttpServletRequest request, Model model) {
		Subject subject = SecurityUtils.getSubject();
		String username = (String) subject.getPrincipal();

		Set<String> permissions = userService.findPermissions(username);
		Map<Long, Resource> map = resourceService.findResources(permissions);

		List<Menu> mainmenu = menuService.findMainMenus(map);
		for (Menu menu : mainmenu) {
			System.out.println(menu.toString());
			for (Menu item : menu.getSubMenus()) {
				System.out.println("		" + item.toString());
			}
		}
		model.addAttribute("mainmenu", mainmenu);
		return ("left");
	}

	@RequestMapping("/top")
	public String loadTop() {
		return ("top");
	}

}
